Skip to content

Conversation

@JohnRichard4096
Copy link
Member

@JohnRichard4096 JohnRichard4096 commented Aug 3, 2025

Sourcery 总结

重构 hook 类型和管理器,集中化货币 UUID 常量,简化 ID 生成,更新依赖并提升版本,并为账户操作添加依赖注入单元测试。

功能增强:

  • 将 HooksType 转换为字符串支持的枚举(Enum),并相应地更新 hook 调用
  • 简化 HooksManager 单例逻辑,通过枚举成员资格验证 hook,并扩展异常处理以包含 DataUpdate
  • 从 action 枚举中移除废弃的验证方法,并将默认货币常量整合到 uuid_lib 中
  • 将手动 UUID 拼接替换为 get_uni_id,以实现一致的 ID 生成

构建:

  • 将包版本提升至 0.1.5,并将 nonebot-adapter-onebot 添加到开发依赖中

测试:

  • 为依赖注入账户执行器添加单元测试,涵盖余额操作和账户删除
Original summary in English

Summary by Sourcery

Refactor hook types and manager, centralize currency UUID constants, streamline ID generation, bump version with updated dependencies, and add dependency-injection unit tests for account operations

Enhancements:

  • Convert HooksType to a string-backed Enum and update hook invocations accordingly
  • Simplify HooksManager singleton logic, validate hooks via Enum membership, and broaden exception handling to include DataUpdate
  • Remove obsolete validation methods from action Enum and consolidate default currency constants in uuid_lib
  • Replace manual UUID concatenation with get_uni_id for consistent ID generation

Build:

  • Bump package version to 0.1.5 and add nonebot-adapter-onebot to dev dependencies

Tests:

  • Add unit tests for dependency-injection account executor covering balance operations and account deletion

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Aug 3, 2025

审阅者指南

此 PR 将 hook 标识符重构为枚举类型,简化了 hook 管理和 UUID 处理,合并了默认货币常量,提升了包版本并添加了新的适配器依赖,同时增加了依赖注入单元测试套件。

重构后的 HooksType 和 HooksManager 的类图

classDiagram
    class HooksType {
        <<Enum>>
        PRE: str = "vault_pre_transaction"
        POST: str = "vault_post_transaction"
        +pre() str
        +post() str
        +methods() list[str]
    }
    class HooksManager {
        -__hooks: dict[str, list[Callable[..., Awaitable[None]]]]
        -_instance
        +__new__()
        +register(hook_name: str, hook_func: Callable[..., Awaitable[None]])
        +run_hooks(hook_name: str, context: Any)
    }
    HooksManager --> HooksType : uses
Loading

更新后的 UUID 和默认货币处理的类图

classDiagram
    class uuid_lib {
        +NAMESPACE_VALUE: UUID
        +DEFAULT_NAME: str
        +DEFAULT_CURRENCY_UUID: UUID
        +to_uuid(s: str) str
        +get_uni_id(id: str, currency_id: str) str
    }
    class CurrencyRepository {
        +DEFAULT_CURRENCY_UUID: UUID
        +DEFAULT_NAME: str
        +NAMESPACE_VALUE: UUID
    }
    class UserAccount {
        +__init__(**kwargs: Any)
    }
    uuid_lib <.. CurrencyRepository : provides constants
    uuid_lib <.. UserAccount : provides get_uni_id, NAMESPACE_VALUE
Loading

清理后的 Method 枚举类图

classDiagram
    class Method {
        <<Enum>>
        DEPOSIT: str
        WITHDRAW: str
        TRANSFER_IN: str
        TRANSFER_OUT: str
    }
Loading

文件级变更

变更 详情 文件
将 HooksType 重构为枚举
  • 将 HooksType 转换为 Enum 和 str 的子类
  • 用 PRE 和 POST 成员替换了私有 __pre/__post 属性
  • 更新了 pre()、post() 和 methods() 以使用枚举成员及其 .value
nonebot_plugin_value/hook/hooks_type.py
用枚举值替换 hook 调用
  • 将 HooksType.pre()/post() 调用替换为 HooksType.PRE.value 和 HooksType.POST.value
  • 在 del_balance、add_balance 和 transfer_funds 中应用了替换
nonebot_plugin_value/services/balance.py
合并默认货币常量
  • 从 repository 中移除了重复的 DEFAULT_NAME 和 DEFAULT_CURRENCY_UUID
  • 导入并暴露了 all 中的默认值
  • 在 uuid_lib.py 中声明了 DEFAULT_NAME 和 DEFAULT_CURRENCY_UUID
nonebot_plugin_value/repository.py
nonebot_plugin_value/uuid_lib.py
简化 HooksManager 单例和错误处理
  • 简化了 new 签名(移除了未使用的参数)
  • 用直接的枚举成员检查替换了 valid_hooks 检查
  • 扩展了异常捕获,以包含 DataUpdate 和 CancelAction
nonebot_plugin_value/hook/hooks_manager.py
移除 Enums 中冗余的 valid_actions
  • 从 Method 枚举中删除了 valid_actions 方法
nonebot_plugin_value/action_type.py
在 balance 模型中使用 uni_id 生成助手
  • 将内联的 uuid5(namespace, id+currency_id) 替换为 get_uni_id 助手
  • 封装了 get_uni_id 调用,以实现更清晰的连接逻辑
nonebot_plugin_value/models/balance.py
提升版本并添加适配器依赖
  • 项目版本提升至 0.1.5
  • 将 nonebot-adapter-onebot 添加到开发依赖项中
pyproject.toml
添加依赖注入单元测试
  • 为通过 DependsSwitch 进行的新货币操作创建了 pytest asyncio 测试
  • 模拟了 MessageEvent 以验证 add_balance 和 get_balance 工作流
tests/value_tests/depends.py

提示和命令

与 Sourcery 交互

  • 触发新评审: 在拉取请求上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的评审评论。
  • 从评审评论生成 GitHub issue: 通过回复评审评论,要求 Sourcery 从该评论创建 issue。您也可以回复评审评论并加上 @sourcery-ai issue 来创建 issue。
  • 生成拉取请求标题: 随时在拉取请求标题中的任意位置写入 @sourcery-ai,以生成标题。您也可以在拉取请求上评论 @sourcery-ai title,以随时(重新)生成标题。
  • 生成拉取请求摘要: 随时在拉取请求正文中的任意位置写入 @sourcery-ai summary,以在您想要的位置生成 PR 摘要。您也可以在拉取请求上评论 @sourcery-ai summary,以随时(重新)生成摘要。
  • 生成审阅者指南: 在拉取请求上评论 @sourcery-ai guide,以随时(重新)生成审阅者指南。
  • 解决所有 Sourcery 评论: 在拉取请求上评论 @sourcery-ai resolve,以解决所有 Sourcery 评论。如果您已经处理了所有评论,并且不想再看到它们,这会很有用。
  • 驳回所有 Sourcery 评审: 在拉取请求上评论 @sourcery-ai dismiss,以驳回所有现有的 Sourcery 评审。如果您想重新开始新的评审,这尤其有用——别忘了评论 @sourcery-ai review 来触发新的评审!

自定义您的体验

访问您的 仪表板 以:

  • 启用或禁用评审功能,例如 Sourcery 生成的拉取请求摘要、审阅者指南等。
  • 更改评审语言。
  • 添加、删除或编辑自定义评审说明。
  • 调整其他评审设置。

获取帮助

Original review guide in English

Reviewer's Guide

This PR refactors hook identifiers into an Enum, streamlines hook management and UUID handling, consolidates default currency constants, bumps the package version with a new adapter dependency, and adds a dependency‐injection unit test suite.

Class diagram for refactored HooksType and HooksManager

classDiagram
    class HooksType {
        <<Enum>>
        PRE: str = "vault_pre_transaction"
        POST: str = "vault_post_transaction"
        +pre() str
        +post() str
        +methods() list[str]
    }
    class HooksManager {
        -__hooks: dict[str, list[Callable[..., Awaitable[None]]]]
        -_instance
        +__new__()
        +register(hook_name: str, hook_func: Callable[..., Awaitable[None]])
        +run_hooks(hook_name: str, context: Any)
    }
    HooksManager --> HooksType : uses
Loading

Class diagram for updated UUID and default currency handling

classDiagram
    class uuid_lib {
        +NAMESPACE_VALUE: UUID
        +DEFAULT_NAME: str
        +DEFAULT_CURRENCY_UUID: UUID
        +to_uuid(s: str) str
        +get_uni_id(id: str, currency_id: str) str
    }
    class CurrencyRepository {
        +DEFAULT_CURRENCY_UUID: UUID
        +DEFAULT_NAME: str
        +NAMESPACE_VALUE: UUID
    }
    class UserAccount {
        +__init__(**kwargs: Any)
    }
    uuid_lib <.. CurrencyRepository : provides constants
    uuid_lib <.. UserAccount : provides get_uni_id, NAMESPACE_VALUE
Loading

Class diagram for Method Enum after cleanup

classDiagram
    class Method {
        <<Enum>>
        DEPOSIT: str
        WITHDRAW: str
        TRANSFER_IN: str
        TRANSFER_OUT: str
    }
Loading

File-Level Changes

Change Details Files
Refactor HooksType to an Enum
  • Converted HooksType to subclass both Enum and str
  • Replaced private __pre/__post attributes with PRE and POST members
  • Updated pre(), post(), and methods() to use enum members and their .value
nonebot_plugin_value/hook/hooks_type.py
Replace hook invocations with Enum values
  • Swapped HooksType.pre()/post() calls for HooksType.PRE.value and HooksType.POST.value
  • Applied replacements across del_balance, add_balance, and transfer_funds
nonebot_plugin_value/services/balance.py
Consolidate default currency constants
  • Removed duplicate DEFAULT_NAME and DEFAULT_CURRENCY_UUID from repository
  • Imported and exposed defaults in all
  • Declared DEFAULT_NAME and DEFAULT_CURRENCY_UUID in uuid_lib.py
nonebot_plugin_value/repository.py
nonebot_plugin_value/uuid_lib.py
Simplify HooksManager singleton and error handling
  • Streamlined new signature (removed unused args)
  • Replaced valid_hooks check with direct enum membership
  • Expanded exception catch to include DataUpdate alongside CancelAction
nonebot_plugin_value/hook/hooks_manager.py
Remove redundant valid_actions in Enums
  • Deleted valid_actions method from Method enum
nonebot_plugin_value/action_type.py
Use helper for uni_id generation in balance model
  • Replaced inline uuid5(namespace, id+currency_id) with get_uni_id helper
  • Wrapped get_uni_id call for clearer concatenation logic
nonebot_plugin_value/models/balance.py
Bump version and add adapter dependency
  • Incremented project version to 0.1.5
  • Added nonebot-adapter-onebot to dev-dependencies
pyproject.toml
Add dependency-injection unit tests
  • Created a pytest asyncio test for new currency operations via DependsSwitch
  • Simulated MessageEvent to verify add_balance and get_balance workflow
tests/value_tests/depends.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JohnRichard4096 - 我已审阅您的更改 - 以下是一些反馈:

  • 在 HooksManager.register 中,if hook_name not in HooksType: 无法正确验证字符串钩子名称——请考虑检查 HooksType._value2member_map_ 或使用 HooksType(hook_name) 进行验证。
  • HooksType 枚举仍然提供 prepost 方法,但大多数代码现在使用 .value;请考虑删除这些方法或重构为单个一致的 API 来检索钩子标识符。
  • 在 HooksType 中添加一个类方法,返回其所有字符串值(例如 .values()),然后将其用于验证和 methods() 中,以避免分散的 .value 访问。
AI 代理提示
请处理此代码审查中的评论:
## 总体评论
- 在 HooksManager.register 中,`if hook_name not in HooksType:` 无法正确验证字符串钩子名称——请考虑检查 `HooksType._value2member_map_` 或使用 `HooksType(hook_name)` 进行验证。
- HooksType 枚举仍然提供 `pre``post` 方法,但大多数代码现在使用 `.value`;请考虑删除这些方法或重构为单个一致的 API 来检索钩子标识符。
- 在 HooksType 中添加一个类方法,返回其所有字符串值(例如 `.values()`),然后将其用于验证和 `methods()` 中,以避免分散的 `.value` 访问。

## 单独评论

### 评论 1
<location> `nonebot_plugin_value/hook/hooks_type.py:4` </location>
<code_context>
+from enum import Enum
+
+
+class HooksType(Enum, str):
+    PRE = "vault_pre_transaction"
+    POST = "vault_post_transaction"
</code_context>

<issue_to_address>
枚举继承顺序应为 (str, Enum) 以获得正确行为。

当前的 (Enum, str) 顺序可能导致类型检查和序列化问题。请切换到 (str, Enum) 以确保正确的字符串行为。
</issue_to_address>

### 评论 2
<location> `nonebot_plugin_value/hook/hooks_type.py` </location>
<code_context>
+    @classmethod
+    def pre(cls) -> str:
+        return cls.PRE
</code_context>

<issue_to_address>
返回枚举成员而不是其值可能会导致问题。

目前,这些方法返回枚举成员而不是其字符串值,这可能会破坏期望字符串的代码。请返回 cls.PRE.value 以保持一致性。
</issue_to_address>

### 评论 3
<location> `nonebot_plugin_value/hook/hooks_type.py` </location>
<code_context>
+    @classmethod
+    def post(cls) -> str:
+        return cls.POST
</code_context>

<issue_to_address>
post() 应返回枚举值,而不是成员。

返回 cls.POST.value 以确保方法返回预期的字符串,与 pre() 的行为匹配。
</issue_to_address>

Sourcery 对开源免费 - 如果您喜欢我们的评论,请考虑分享它们 ✨
帮助我更有用!请点击 👍 或 👎 对每个评论进行反馈,我将使用反馈来改进您的评论。
Original comment in English

Hey @JohnRichard4096 - I've reviewed your changes - here's some feedback:

  • In HooksManager.register, if hook_name not in HooksType: won’t correctly validate string hook names—consider checking against HooksType.value2member_map or using HooksType(hook_name) for validation.
  • The HooksType enum still provides pre and post methods but most code now uses .value; consider removing those methods or refactoring to a single consistent API for retrieving hook identifiers.
  • Add a classmethod to HooksType that returns all its string values (e.g. .values()), then use it for validation and in methods() to avoid scattered .value access.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In HooksManager.register, `if hook_name not in HooksType:` won’t correctly validate string hook names—consider checking against HooksType._value2member_map_ or using `HooksType(hook_name)` for validation.
- The HooksType enum still provides `pre` and `post` methods but most code now uses `.value`; consider removing those methods or refactoring to a single consistent API for retrieving hook identifiers.
- Add a classmethod to HooksType that returns all its string values (e.g. `.values()`), then use it for validation and in `methods()` to avoid scattered `.value` access.

## Individual Comments

### Comment 1
<location> `nonebot_plugin_value/hook/hooks_type.py:4` </location>
<code_context>
+from enum import Enum
+
+
+class HooksType(Enum, str):
+    PRE = "vault_pre_transaction"
+    POST = "vault_post_transaction"
</code_context>

<issue_to_address>
Enum inheritance order should be (str, Enum) for correct behavior.

The current (Enum, str) order can cause issues with type checks and serialization. Please switch to (str, Enum) to ensure correct string behavior.
</issue_to_address>

### Comment 2
<location> `nonebot_plugin_value/hook/hooks_type.py` </location>
<code_context>
+    @classmethod
+    def pre(cls) -> str:
+        return cls.PRE
</code_context>

<issue_to_address>
Returning enum members instead of their values may cause issues.

Currently, these methods return the enum member instead of its string value, which may break code expecting a string. Please return cls.PRE.value for consistency.
</issue_to_address>

### Comment 3
<location> `nonebot_plugin_value/hook/hooks_type.py` </location>
<code_context>
+    @classmethod
+    def post(cls) -> str:
+        return cls.POST
</code_context>

<issue_to_address>
post() should return the enum value, not the member.

Return cls.POST.value to ensure the method returns the expected string, matching the behavior of pre().
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@JohnRichard4096 JohnRichard4096 merged commit f76de47 into master Aug 3, 2025
1 check passed
@JohnRichard4096 JohnRichard4096 deleted the feat-upd-code branch August 3, 2025 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants